﻿using Infra.CustomException.Resources;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infra.CustomException
{
    public abstract class BaseException : Exception
    {
        public new Exception InnerException { get; protected set; }
        public DateTime Date { get; protected set; }
        public TimeSpan Time { get; protected set; }

        protected static LoggerManager Logger { get; set; }
        protected string _message;

        protected BaseException(Exception exception)
        {
            this.InnerException = exception;
            this.Date = DateTime.Now;
            this.Time = DateTime.Now.TimeOfDay;

            if(Logger != null)
                Logger.Error(exception);
        }

        protected BaseException(string message)
            : this(new Exception(message))
        {
            this._message = message;
        }

        public string GetMessage(Exception exception, CultureInfo culture)
        { 
            var result = "";
            if (exception is SqlException)
                result = this.GetMessage(exception as SqlException, culture);

            return result;
        }

        public string GetMessage(SqlException exception, CultureInfo culture)
        {
            var result = "";
            switch (exception.Number)
            {
                case -2:
                    result = DalResource.ResourceManager.GetString(DalResource.time_out, culture);
                    break;
                case 2627:
                    result = DalResource.ResourceManager.GetString(DalResource.duplicate_key, culture);
                    break;
            }

            return result;
        }
    }
}
